package base.utils.pay;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

public class Pu {
//	public static String paykey= "258wsd5fsdeb5h5jcuts5458sd5f54f5";
//	public static String paykey= "huatongchuanmeihuatongchuanmeihu";
	public static String paykey= "tygyjztygyjztygyjztygyjztygyjz12";
	/*public static String paykey= "tongzhonglianheweixinzhifumiyao0";*/
	 public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters){
	        StringBuffer sb = new StringBuffer();
	        Set es = parameters.entrySet();
	        Iterator it = es.iterator();
	        while(it.hasNext()) {
	            Map.Entry entry = (Map.Entry)it.next();
	            String k = (String)entry.getKey();
	            Object v = entry.getValue();
	            if(null != v && !"".equals(v) 
	                    && !"sign".equals(k) && !"key".equals(k)) {
	                sb.append(k + "=" + v + "&");
	            }
	        }
	        sb.append("key="+paykey);
	        String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
	        System.out.println(paykey);
	        return sign;
	    }
	 public static String sign(String characterEncoding,SortedMap<Object,Object> parameters){
	        StringBuffer sb = new StringBuffer();
	        Set es = parameters.entrySet();
	        Iterator it = es.iterator();
	        while(it.hasNext()) {
	            Map.Entry entry = (Map.Entry)it.next();
	            String k = (String)entry.getKey();
	            Object v = entry.getValue();
	            if(null != v && !"".equals(v) 
	                    && !"sign".equals(k) && !"key".equals(k)) {
	                sb.append(k + "=" + v + "&");
	            }
	        }
	        sb.append("key="+paykey);
	        String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
	        return sign;
	    }
	 /**
	      * @author 李欣桦
	      * @date 2014-12-5下午2:32:05
	      * @Description：将请求参数转换为xml格式的string
	      * @param parameters  请求参数
	      * @return
	      */
	     public static String getRequestXml(SortedMap<Object,Object> parameters){
	         StringBuffer sb = new StringBuffer();
	         
	         sb.append("<xml>");
	         Set es = parameters.entrySet();
	         Iterator it = es.iterator();
	         while(it.hasNext()) {
	             Map.Entry entry = (Map.Entry)it.next();
	             String k = (String)entry.getKey();
	             String v = (String)entry.getValue();
	             if ("attach".equalsIgnoreCase(k)||"body".equalsIgnoreCase(k)||"sign".equalsIgnoreCase(k)) {
	                 sb.append("<"+k+">"+"<![CDATA["+v+"]]></"+k+">");
	             }else {
	                 sb.append("<"+k+">"+v+"</"+k+">");
	             }
	         }
	         sb.append("</xml>");
	         return sb.toString();
	     }
	     public static void main(String[] args) {
	    	 String time=Long.toString((new Date().getTime()/1000));
	    	 System.out.println(time);
		}

	    		public static String SHA1(String decript) {
	    			try {
	    				MessageDigest digest = java.security.MessageDigest
	    						.getInstance("SHA-1");
	    				digest.update(decript.getBytes());
	    				byte messageDigest[] = digest.digest();
	    				// Create Hex String
	    				StringBuffer hexString = new StringBuffer();
	    				// 字节数组转换为 十六进制 数
	    				for (int i = 0; i < messageDigest.length; i++) {
	    					String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
	    					if (shaHex.length() < 2) {
	    						hexString.append(0);
	    					}
	    					hexString.append(shaHex);
	    				}
	    				return hexString.toString();

	    			} catch (NoSuchAlgorithmException e) {
	    				e.printStackTrace();
	    			}
	    			return "";
	    		}
	    		
}
